package java8.test;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class MethodParameters {
    public static void main(String[] args) {
        MethodParameters mp = new MethodParameters();

        try {
            java.lang.reflect.Type f;
            Method m1 = String.class.getDeclaredMethod("regionMatches",
                    boolean.class, int.class, String.class, int.class, int.class);
            mp.printParameterNames(m1);
            mp.printParameterNamesPre8(m1);

            Method m2 = MethodParameters.class.getDeclaredMethod("someGreatMethod",
                    Number.class, int.class, float.class);
            mp.printParameterNames(m2);
            mp.printParameterNamesPre8(m2);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }

    }

    /**
     * 经测试 命令行模式 使用-parameters编译 有效果
     */
    public void someGreatMethod(Number xParam, int yParam, float zParam) {
    }

    // using new Java 8 API
    public void printParameterNames(Method m) {
        System.err.print(m.getName() + "(");
        for (Parameter p : m.getParameters()) {
            System.err.print(" " + p.getName());
        }
        System.err.println(")");
    }

    // Java8之前的API
    public void printParameterNamesPre8(Method m) {
        System.err.print(m.getName() + "(");
        int paramCount = m.getParameterTypes().length;
        for (int i = 0; i < paramCount; i++) {
            System.err.print(" arg" + i);
        }
        System.err.println(")");
    }
}